package at.guigu.controller;

import at.guigu.pojo.Book;
import at.guigu.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

// @RestController = @Controller+ @ResponseBody
@RestController
// Restful风格
@RequestMapping(value = "/books")
public class BookController {
    @Autowired
    private BookService bookService;

    /*-----------------------查询数据-------------------------*/
    // 查询所有数据
    @GetMapping
    public Result getAll() {
        List<Book> books = bookService.getAll();
        Integer code = books != null ? Code.SELETE_OK : Code.SELETE_ERR;
        String message = books != null ? "" : "数据查询失败，请重试";
        return new Result(code, books, message);
    }
    // 查询单条数据：通过id查询
    @GetMapping("/{id}")
    public Result getById(@PathVariable(value = "id") Integer id) {
        Book book = bookService.getById(id);
        // int i = 1/0;测试异常使用
        Integer code = book != null ? Code.SELETE_OK : Code.SELETE_ERR;
        String message = book != null ? "" : "数据查询失败，请重试";
        return new Result(code, book, message);
    }
    // 静态单条件查询
    @GetMapping("/singleConOne/{id}")
    public Result selectBySingleConOne(@PathVariable(value = "id") Integer id) {
        List<Book> books = bookService.selectBySingleConOne(id);
        Integer code = books != null ? Code.SELETE_OK : Code.SELETE_ERR;
        String message = books != null ? "" : "数据查询失败，请重试";
        return new Result(code, books, message);
    }
    // 动态单条件查询——对象参数接收
    @GetMapping("/singleConTwo")
    public Result selectBySingleConTwo(@RequestParam("type") String type, @RequestParam("name") String name, @RequestParam("description") String description) {
        Book book = new Book();
        book.setType(type);
        book.setName(name);
        book.setDescription(description);
        List<Book> books = bookService.selectBySingleConTwo(book);
        Integer code = books != null ? Code.SELETE_OK : Code.SELETE_ERR;
        String message = books != null ? "" : "数据查询失败，请重试";
        return new Result(code, books, message);
    }
    // 动态单条件查询——Map集合参数接收
    @GetMapping("/singleConTwoo")
    public Result selectBySingleConTwoo(@RequestParam("map") Map map) {
        List<Book> books = bookService.selectBySingleConTwoo(map);
        Integer code = books != null ? Code.SELETE_OK : Code.SELETE_ERR;
        String message = books != null ? "" : "数据查询失败，请重试";
        return new Result(code, books, message);
    }
    // 动态多条件查询——对象参数接收
    @GetMapping("/maxConOne")
    public Result selectByMaxConOne(@RequestParam(value = "type", required = false) String type, @RequestParam(value = "name", required = false) String name, @RequestParam(value = "description", required = false) String description) {
        Book book = new Book();
        book.setType(type);
        book.setName(name);
        book.setDescription(description);
        List<Book> books = bookService.selectByMaxConOne(book);
        Integer code = books != null ? Code.SELETE_OK : Code.SELETE_ERR;
        String message = books != null ? "" : "数据查询失败，请重试";
        return new Result(code, books, message);
    }
    // 动态多条件查询——Map集合参数接收
    @GetMapping("/maxConTwo")
    public Result selectByMaxConTwo(@RequestParam("map") Map map) {
        List<Book> books = bookService.selectByMaxConTwo(map);
        Integer code = books != null ? Code.SELETE_OK : Code.SELETE_ERR;
        String message = books != null ? "" : "数据查询失败，请重试";
        return new Result(code, books, message);
    }
    /*-----------------------添加/保存数据-------------------------*/
    @PostMapping
    public Result save(@RequestBody Book book) {
        boolean flag = bookService.save(book);
        return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR, flag);
    }

    /*-----------------------更新/修改数据-------------------------*/
    @PutMapping
    public Result update(@RequestBody Book book) {
        boolean flag = bookService.update(book);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR, flag);
    }

    /*-----------------------删除数据-------------------------*/
    // 单条删除
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable(value = "id") Integer id) {
        boolean flag = bookService.deleteById(id);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR, flag);
    }
    // 批量删除
    @DeleteMapping
    public Result deleteByIds(@RequestParam("ids") Integer[] ids) {
        boolean flag = bookService.deleteByIds(ids);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR, flag);
    }
}
